var toUTF8 = require('./ToUTF8');
var pool = require('./DBConnectPool');
var logger = require('../../common/Logger/Logger');
var redisTool = require('../../common/utils/redistools.js');

module.exports.getOauth = function (openid, callBack) {
    // setAccessToken: function (data, callBack) {
        // setAccessToken:function (key,data,callBack) {
        var key='oauth-'+openid;
        redisTool.getHgetall(key,toUTF8(function (err, result) {
            if (err) {
                console.log('get  from redis failed in getOauth: ', err);
                callBack({
                    isSuccess: false,
                    err: err
                });
                return;
            } else {
                callBack({
                    isSuccess: true,
                    result: result
                });
                console.log({
                    isSuccess: true,
                    result: result
                });
            }
        }));
};

module.exports.setOauth = function (openid, token, callback) {
    var key='oauth-'+openid;
    redisTool.setHset(key, token, toUTF8(function (err, result) {
        if (err) {
            console.log('get connection from pool failed in getOauth: ', err);
            callBack({
                isSuccess: false,
                err: err
            });
            return;
        } else {
            callBack({
                isSuccess: true,
                result: result
            });
            console.log({
                isSuccess: true,
                result: result
            });
            // logger.info({
            //     message: 'access_token update',
            //     sql: "save accessToken to redis"
            // });
        }
    }));
};